package com.alibaba.springboot.health.service.impl;

import com.alibaba.springboot.health.dao.PermissionDao;
import com.alibaba.springboot.health.pojo.Permission;
import com.alibaba.springboot.health.service.PermissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (Permission)表服务实现类
 *
 * @author makejava
 * @since 2019-12-24 14:33:36
 */
@Service("permissionService")
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;

    /**
     * 通过ID查询单条数据
     *
     * @param permissionId 主键
     * @return 实例对象
     */
    @Override
    public Permission queryById(Integer permissionId) {
        return permissionDao.queryById(permissionId);
    }

    /**
     * 通过用户名查询权限
     * @param userName
     * @return
     */
    @Override
    public List<Permission> queryByUserName(String userName) {
        return permissionDao.queryByUserName(userName);
    }


    /**
     * 查询多条数据
     * @param page 页码
     * @param pageSize 页大小
     * @param permission 实体
     * @return PageInfo<Permission> 分页数据集合
     */
    @Override
    public PageInfo<Permission> queryAll(int page, int pageSize, Permission permission) {
        /*添加分页信息*/
        PageHelper.startPage(page, pageSize);

        List<Permission> permissionList = permissionDao.queryAll(permission);

        return new PageInfo<>(permissionList);
    }

    /**
     * 新增数据
     *
     * @param permission 实例对象
     * @return 实例对象
     */
    @Override
    public Permission insert(Permission permission) {
        permissionDao.insert(permission);
        return permission;
    }

    /**
     * 修改数据
     *
     * @param permission 实例对象
     * @return 实例对象
     */
    @Override
    public Permission update(Permission permission) {
        permissionDao.update(permission);
        return queryById(permission.getPermissionId());
    }

    /**
     * 通过主键删除数据
     *
     * @param permissionId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer permissionId) {
        return permissionDao.deleteById(permissionId) > 0;
    }
}